package com.cip.master.action;

import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.HashSet;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;


import com.cip.admin.model.User;
import com.cip.common.util.SecurityUtil;
import com.cip.master.model.ClassYear;
import com.cip.master.model.Subject;
import com.cip.master.model.Examination;
import com.cip.master.model.ExamPaper;
import com.cip.master.service.ClassYearService;
import com.cip.master.service.ExaminationService;
import com.cip.master.service.SubjectService;
import com.cip.webapp.action.BaseAction;

import javax.servlet.http.HttpServletRequest;

public class ExaminationAction extends BaseAction  {
	
	private ExaminationService examService;
	private ClassYearService classYearService;
	private SubjectService  subjectService;
	private Examination exam;
	private List<Examination> examList;
	private List<ClassYear> classYears;
	private List<Subject> subjectList;
	private String classYear;
	private Set<ExamPaper> examPapers;
	private ExamPaper examPaper;
	
	public String saveOrUpdateExam() throws Exception {
		User user = SecurityUtil.getThreadLocalUser().get();
		ClassYear classYr = classYearService.getClassYear(Integer.parseInt(classYear));
		exam.setClassYear(classYr);
		exam.setTenantId(user.getTenantId());
		exam=examService.saveOrUpdateExam(exam);
//		System.out.println(examPaper.getMaxMark());
		HttpServletRequest request = getRequest();
		Set<ExamPaper> examPapers=new HashSet<ExamPaper>();
		String subjectIds[] = request.getParameterValues(
				"subjectIds");
		String maxMarks[] = request.getParameterValues(
				"maxMarks");
		String dates[] = request.getParameterValues(
				"dates");
		for (int i = 0; i < subjectIds.length; i++) {

			ExamPaper examPaper  = new ExamPaper();
			examPaper.setMaxMark(maxMarks[i]);
			examPaper.setSubjectId(subjectIds[i]);
			examPaper.setDate(new Date(dates[i]));
			examPaper.setExamination(exam);
			examPapers.add(examPaper);
		}
		exam.setExamPapers(examPapers);
		examService.saveOrUpdateExam(exam);
		saveMessage(getText("exam.added"));
		return SUCCESS;
	}

	public String showExamList() throws Exception {
		Locale locale = getRequest().getLocale();
		String examString = examService.getExamGridList(locale);
				
		getRequest().setAttribute("examGridString", examString);
		
		
//		setExamList(examService.getAllExamList());
		return SUCCESS;
	}

	public String showEditExam() {
		try {
			exam=examService.getExamById(exam.getId());
			setClassYear(exam.getClassYear().getName());
			setExam(exam);
			setClassYears(classYearService.getAllClassYear());
			setSubjectList(subjectService.getAllSubjectList());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String showCreateExam() throws Exception {
		setClassYears(classYearService.getAllClassYear());
		setSubjectList(subjectService.getAllSubjectList());
		setExam(new Examination());
		return SUCCESS;
	}
	public String showExamsByCandidate() {
		try{
		User user = SecurityUtil.getThreadLocalUser().get();
		setExamList(examService.getAllExamListByClass(user.getClassYear()));
		
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public ExaminationService getExamService() {
		return examService;
	}

	public void setExamService(ExaminationService examService) {
		this.examService = examService;
	}
	

	public ClassYearService getClassYearService() {
		return classYearService;
	}

	public void setClassYearService(ClassYearService classYearService) {
		this.classYearService = classYearService;
	}

	public Examination getExam() {
		return exam;
	}

	public void setExam(Examination exam) {
		this.exam = exam;
	}

	public List<Examination> getExamList() {
		return examList;
	}

	public void setExamList(List<Examination> examList) {
		this.examList = examList;
	}

	public List<ClassYear> getClassYears() {
		return classYears;
	}

	public void setClassYears(List<ClassYear> classYears) {
		this.classYears = classYears;
	}

	public String getClassYear() {
		return classYear;
	}

	public void setClassYear(String classYear) {
		this.classYear = classYear;
	}

	public SubjectService getSubjectService() {
		return subjectService;
	}

	public void setSubjectService(SubjectService subjectService) {
		this.subjectService = subjectService;
	}

	public List<Subject> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<Subject> subjectList) {
		this.subjectList = subjectList;
	}

	public Set<ExamPaper> getExamPapers() {
		return examPapers;
	}

	public void setExamPapers(Set<ExamPaper> examPapers) {
		this.examPapers = examPapers;
	}

	public ExamPaper getExamPaper() {
		return examPaper;
	}

	public void setExamPaper(ExamPaper examPaper) {
		this.examPaper = examPaper;
	}	

}
